package dcdroplet;

// Import Java JDK Classes
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

/**
 * Encapsulates the properties for a single appender defined from dcdroplet.properties<br/>
 * @author dca
 */
public abstract class AppenderProperties {
	/** The date format for logging message formats.<br/> */
	protected static final SimpleDateFormat DATE = new SimpleDateFormat("MM/dd/yyyy");

	/** The time format for logging message formats.<br/> */
	protected static final SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss a z");

	/** The system line separator.<br/> */
	protected static final String NL = System.getProperty("line.separator");

	private String nameDefinition;
	private String tagsDefinition;
	private String outDefinition;
	private String patternDefinition;
	private String fileDefinition;
	private String sizeDefinition;

	/** Constructor<br/> */
	public AppenderProperties() {
		nameDefinition = "";
		tagsDefinition = "";
		outDefinition = "";
		patternDefinition = "";
		fileDefinition = "";
		sizeDefinition = "";
	} // END constructor

	// Getters methods

	public String getNameDefinition() {
		return nameDefinition;
	} // END getNameDefinition

	public String getTagsDefinition() {
		return tagsDefinition;
	} // END getTagsDefinition

	public String getOutDefinition() {
		return outDefinition;
	} // END getOutDefinition

	public String getPatternDefinition() {
		return patternDefinition;
	} // END getPatternDefinition

	public String getFileDefinition() {
		return fileDefinition;
	} // END getFileDefinition

	public String getSizeDefinition() {
		return sizeDefinition;
	} // END getSizeDefinition

	// Setters methods

	public void setNameDefinition(String nameDefinition) {
		if(null != nameDefinition) {
			this.nameDefinition = nameDefinition;
		} // END if
	} // END setNameDefinition

	public void setTagsDefinition(String tagsDefinition) {
		if(null != tagsDefinition) {
			this.tagsDefinition = tagsDefinition;
		} // END if
	} // END setTagsDefinition

	public void setOutDefinition(String outDefinition) {
		if(null != outDefinition) {
			this.outDefinition = outDefinition;
		} // END if
	} // END setOutDefinition

	public void setPatternDefinition(String patternDefinition) {
		if(null != patternDefinition) {
			this.patternDefinition = patternDefinition;
		} // END if
	} // END setPatternDefinition

	public void setFileDefinition(String fileDefinition) {
		if(null != fileDefinition) {
			this.fileDefinition = fileDefinition;
		} // END if
	} // END setFileDefinition

	public void setSizeDefinition(String sizeDefinition) {
		if(null != sizeDefinition) {
			this.sizeDefinition = sizeDefinition;
		} // END if
	} // END setSizeDefinition

	// Has methods

	public boolean hasNameDefinition() {
		return !nameDefinition.isEmpty();
	} // END hasNameDefinition

	public boolean hasTagsDefinition() {
		return !tagsDefinition.isEmpty();
	} // END hasTagsDefinition

	public boolean hasOutDefinition() {
		return !outDefinition.isEmpty();
	} // END hasOutDefinition

	public boolean hasPatternDefinition() {
		return !patternDefinition.isEmpty();
	} // END hasPatternDefinition

	public boolean hasFileDefinition() {
		return !fileDefinition.isEmpty();
	} // END hasFileDefinition

	public boolean hasSizeDefinition() {
		return !sizeDefinition.isEmpty();
	} // END hasSizeDefinition

	// Auxiliary methods

	public String getAppenderName() {
		return nameDefinition;
	} // END getAppenderName

	public Set<String> getTagSet() {
		return new TreeSet<String>(Arrays.asList(tagsDefinition.split(",")));
	} // END getTagSet

	/**
	 * Returns true if the properties file has tags supplied in dcdroplet.<APPENDER NAME>.properties and those tags contain the supplied tag.<br/>
	 * @param tag The tag name.<br/>
	 * @return true or false.<br/>
	 */
	public boolean hasTag(String tag) {
		return Util.containsTag(getTagSet(), tag);
	} // END hasTag

	public File getFile() {
		return new File(fileDefinition);
	} // END getFile

	public String getSize() {
		return sizeDefinition;
	} // END getSize

	public int getSizeAsInt() {
		try {
			return Integer.parseInt(sizeDefinition);
		} catch(Exception exc) {
			return 0;
		} // END try/catch
	} // END getSizeAsInt

	public long getSizeAsLong() {
		try {
			return Long.parseLong(sizeDefinition);
		} catch(Exception exc) {
			return 0L;
		} // END try/catch
	} // END getSizeAsLong

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer(1024);

		buffer.append("{");
		buffer.append("name = ");
		buffer.append(nameDefinition);
		buffer.append(", ");
		buffer.append("tags = [");
		buffer.append(tagsDefinition);
		buffer.append("], ");
		buffer.append("out = ");
		buffer.append(outDefinition);
		buffer.append(", ");
		buffer.append("pattern = ");
		buffer.append(patternDefinition);
		buffer.append(", ");
		buffer.append("file = ");
		buffer.append(fileDefinition);
		buffer.append(", ");
		buffer.append("size = ");
		buffer.append(sizeDefinition);
		buffer.append("}");

		return buffer.toString();
	} // END toString

	public abstract void log(String tag, String msg, String msgFmt, String className, long timeStamp);
} // END class AppenderProperties
